﻿#region Using Directives

    using System;
    using System.Linq;
    using System.Xml.Linq;

    using Csla;

    using MarketPulse.BusinessLogic.Core;

#endregion

namespace MarketPulse.Reporting.Core
{
    /// <summary>
    /// This class represents a read only instance of a ReportingPeriod within the system.
    /// </summary>
    [Serializable]    
    public class ReportingPeriodInfo : MarketPulseReadOnlyBase<ReportingPeriodInfo>
    {

        #region Constructors

            /// <summary>
            /// Default constructor is private in order to (try to) enforce construction
            /// only via the static factory methods provided.
            /// </summary>
            private ReportingPeriodInfo()
            {
            }

        #endregion       

        #region Properties

            #region Association GUID Properties (Foreign Keys)

                /// <summary>
                /// The GUID of the company (BusinessObject) that this Reporting Period  belongs to 
                /// </summary>
                public static PropertyInfo<Nullable<Guid>> CompanyGUIDProperty = RegisterProperty<Nullable<Guid>>(p => p.CompanyGUID);
                /// <summary>
                /// The GUID of the company (BusinessObject) that this Reporting Period  belongs to 
                /// </summary>
                public Nullable<Guid> CompanyGUID
                {
                    get { return GetProperty(CompanyGUIDProperty); }
                }

                /// <summary>
                /// The GUID of the type for the Reporting Period 
                /// </summary>
                public static PropertyInfo<Nullable<Guid>> ReportingPeriodTypeGUIDProperty = RegisterProperty<Nullable<Guid>>(p => p.ReportingPeriodTypeGUID);
                /// <summary>
                /// The GUID of the type for the Reporting Period 
                /// </summary>
                public Nullable<Guid> ReportingPeriodTypeGUID
                {
                    get { return GetProperty(ReportingPeriodTypeGUIDProperty); }
                }    

            #endregion

            /// <summary>
            /// The language code to use when retrieving localised text from the ReportingPeriodInfo.
            /// </summary>
            public string Language { get; set; }

            /// <summary>
            /// A human readable name for this Reporting Period 
            /// </summary>
            public static PropertyInfo<string> ReportingPeriodNameProperty = RegisterProperty<string>(p => p.Name);
            public string Name
            {
                get 
                {
                    XElement nameXml = XElement.Parse(GetProperty(ReportingPeriodNameProperty));

                    return (from l in nameXml.Elements("text")
                            where l.Attribute("language").Value.Equals(Language)
                            select l).FirstOrDefault().Value;
                }
            }

            /// <summary>
            /// The date and time when the Reporting Period  starts
            /// </summary>
            public static PropertyInfo<SmartDate> EffectiveStartDateTimeProperty = RegisterProperty<SmartDate>(p => p.EffectiveStartDateTime);
            public SmartDate EffectiveStartDateTime
            {
                get { return GetProperty(EffectiveStartDateTimeProperty); }
            }

            /// <summary>
            /// The date and time when the Reporting Period  ends
            /// </summary>
            public static PropertyInfo<SmartDate> EffectiveEndDateTimeProperty = RegisterProperty<SmartDate>(p => p.EffectiveEndDateTime);
            public SmartDate EffectiveEndDateTime
            {
                get { return GetProperty(EffectiveEndDateTimeProperty); }
            }

        #endregion
    }
}
